package com.zim.ta_service.base.aspectj;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * author: alabimofa <br>
 * date: 2021/10/27 19:06 <br>
 */
@SuppressWarnings("ALL")
@Component
@Aspect
public class MybatisAspectj {

	// 配置织入点
	@Pointcut("execution(public * com.baomidou.mybatisplus.core.mapper.BaseMapper.selectOne(..))")
	public void selectOneAspect() {
	}

	/**
	 * SelectOne方法默认添加"limit 1"条件
	 * @param point
	 */
	@Before("selectOneAspect()")
	public void beforeSelect(JoinPoint point) {
		Object arg = point.getArgs()[0];
		if (arg instanceof AbstractWrapper) {
			AbstractWrapper wrapper = (AbstractWrapper) arg;

			String sqlSegment = wrapper.getSqlSegment();
			if (!StrUtil.contains(sqlSegment, "limit")) {
				wrapper.last("limit 1");
			}
		}
	}
}
